<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>部门管理----审批人</title>
    <script type="text/javascript">
      var editDepartmentWin;
      var newDepartmentWin;
      var departmentId;
      Ext.onReady(function(){
        //产品分类列表------------------------------------------------------

        var departmentName =  new Ext.form.TextField ({
          name: 'name',
          width:100,
          id: 'name'
        })
            
        var departmentDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/department/getDepartmentsList'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'name'
            }, {
              name: 'manager',
              mapping:'manager',
              convert:function(v){
                if(v.length==0){
                  return "暂无"
                }else{
                  return v.name;
                }
              }
            }])
        });
        var departmentSM = new Ext.grid.CheckboxSelectionModel();
        var departmentCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer(),departmentSM,

          {
            header: '部门名称',
            dataIndex: 'name',
            width:280
          },{
            header: '部门主管人',
            dataIndex: 'manager',
            width:280
          }]);


        var departmentGrid =  new Ext.grid.GridPanel({
          ds: departmentDS,
          cm: departmentCM,
          sm: departmentSM,
          width: screenWidth-325,
          height: screenHeight-338,
          iconCls: 'icon-grid',loadMask: true,
          tbar:[{
              text:'添加部门',
              width:50,
              iconCls:'addItem',
              handler:function(){
                newDepartmentWin = new Ext.Window({
                  layout: 'fit',
                  width:317,
                  height:117,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //autoScroll:true,
                  //modal: true,
                  title: '新增部门情况',
                  autoLoad: {
                    url: "/department/newDepartment",
                    scripts: true
                  }
                });
                newDepartmentWin.on("destroy",function(){
                  departmentDS.reload();
                });
                newDepartmentWin.resizable = false;
                newDepartmentWin.show();
              }
            },'-',{
              text: '编辑部门',
              width:50,
              iconCls:'editItem',
              handler:function(){
                var records = departmentGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  //把表单添加到窗口中
                  departmentId = records[0].get('id');
                  editDepartmentWin = new Ext.Window({
                    title: '编辑部门情况',
                    layout:'fit',
                    width:317,
                    height:143,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    //autoScroll:true,
                    autoLoad: {
                      url: "/department/editDepartment?id=" + departmentId,
                      scripts: true
                    }
                  });
                  editDepartmentWin.on("destroy",function(){
                    departmentDS.reload();
                  });
                  editDepartmentWin.resizable = false;
                  editDepartmentWin.show();
                }
              }
            },'-',{
              text:'删除部门',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(departmentGrid.getSelectionModel().hasSelection()){
                  var rs = departmentGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var name='';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    name+=data.name+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);
                  Ext.MessageBox.confirm('警告','确定删除以下部门？<br><font color="red">' + name + '</font>',function (button){
                    if(button=='yes'){
                      Ext.Ajax.request({
                        url:'/department/delete',
                        method:'post',
                        success:function(response,opts){
                          var data = Ext.util.JSON.decode(response.responseText);
                          if(data.success&&data.info=='success') {
                            departmentDS.reload();
                            Ext.Msg.alert('提示信息', '已成功删除。');
                          } else {
                            Ext.Msg.alert('提示信息', data.info);
                          }
                        },
                        params: {"id":keys}
                      });
                    }
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要删除的部门');
                }
              }
            },'-','检索条件','-',
            '部门名称',departmentName
            ,{
              text:'搜索',
              iconCls:'search',
              handler:function(){
                departmentDS.baseParams.name = departmentName.getValue();
                departmentDS.load({
                  params:{
                    start: 0,
                    limit: pageSize
                  }
                })
              }
            }],
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: departmentDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
        });
        departmentGrid.render('department_list');
        departmentDS.load();

      })

    </script>
  </head>

  <body>
    <div id="department_list"></div>
  </body>
</html>